package com.lam.common.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.lam.common.base.domain.Result;
import com.lam.common.components.captcha.enums.CaptchaCategoryEnum;
import com.lam.common.components.captcha.enums.CaptchaTypeEnum;
import com.lam.common.components.captcha.service.CaptchaService;
import com.lam.common.components.parameter.ParameterConfigUtil;
import com.lam.common.utils.IpUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.framework.operLog.enums.LogSave;

/**
 * 验证码操作处理
 * 
 * @author lam
 */
@Log(title = "验证码操作")
@RestController
@RequestMapping("/${adminPath}/captcha")
public class CaptchaController {
	
	@Resource(name = "sysCaptchaService")
	private CaptchaService sysCaptchaService;
	
	@Autowired
    private com.anji.captcha.service.CaptchaService anjiCaptchaService;
	
	/**
     * 获取验证码开关
     */
	@Log(oper = "获取验证码开关")
    @GetMapping("/getOpen")
    public Result getOpen() {
		Boolean open = ParameterConfigUtil.getBoolean("captcha.open", true);
		Map<String, Object> json = new HashMap<>();
		json.put("open", open);
		if(open) {
			CaptchaTypeEnum captchaType = sysCaptchaService.getCaptchaType(null);
			json.put("category", captchaType.getCategory().getName());
			if(captchaType.getCategory() == CaptchaCategoryEnum.PICTURE) {
				json.put("captchaType", captchaType.getName());
			}
		}
        return Result.ok(json);
    }

	/**
	 * 生成字符类型验证码
	 */
	@Log(businessType = BusinessType.LOGIN_CAPTCHA_CODE, oper = "生成字符验证码", logSave = LogSave.YES, saveResData = false)
	@GetMapping("/code")
	public Result genCode(@RequestParam("username")String username) {
		
		ValidateUtil.validRequired(username, "用户名不能为空！");
		
		return Result.ok(sysCaptchaService.genCode(username));
	}
	
	/**
	 * 生成图片类型验证码
	 */
	@Log(businessType = BusinessType.LOGIN_CAPTCHA_IMG, oper = "生成图形验证码", logSave = LogSave.YES, saveResData = false)
	@PostMapping("/pic")
	public Result genPic(@RequestBody CaptchaVO data, HttpServletRequest request) {
		
        data.setBrowserInfo(getRemoteId(request));
        ResponseModel res = anjiCaptchaService.get(data);
        return res.isSuccess()?Result.ok(res.getRepData()):Result.fail(res.getRepMsg());
	}
	
	private String getRemoteId(HttpServletRequest request) {
        return IpUtils.getIpAddr(request) + request.getHeader("user-agent");
    }
	
	@Log(oper = "图形验证码校验", logSave = LogSave.YES, saveResData = false)
	@PostMapping("/pic/check")
    public Result checkPic(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(getRemoteId(request));
        ResponseModel res = anjiCaptchaService.check(data);
        return res.isSuccess()?Result.ok(res.getRepData()):Result.fail(res.getRepMsg());
    }
	
	
	
	
}
